import { Base } from "../../utils/base.js";
var WxParse = require('../../utils/wxParse/wxParse.js');
import { ProductList } from '../product/list-model.js';
import { Author } from '../author/author-model.js';
var base = new Base();
var author = new Author();
var productlist = new ProductList();
var tit = '';
Page({
  data: {
    isFlod: true,
    btnChange: true,
    content: "",
    page: 1,
    loadingHidden: false,
    searchLoading: false,
    searchLoadingComplete: false,
  },
  onLoad: function (options) {
    var id = options.id;
    var that = this;
    author.getAuthorDetail(id, (data) => {
      tit = data.title;
      that.setData({
        articleInfo: data,
        loadingHidden: true,
      });
      WxParse.wxParse('content', 'html', data.data.content, that, 5)
      //获得文章列表信息
      this.data.params = { 'num': 10, 'author': tit };
      productlist.getProductData(1, '', this.data.params, (data) => {
        if (data._meta.pageCount == 1) {
          that.setData({
            searchLoadingComplete: true,
          })
        }
        that.setData({
          productData: data.items,
          pageCount: data._meta.pageCount,
        })
      })
    })
  },

  //触底滚动加载
  onReachBottom: function () {
    var page = parseInt(this.data.page) + 1;
    if (page <= this.data.pageCount) {
      this.setData({
        searchLoading: true,
      })

      productlist.getProductData(page,'', this.data.params, (res) => {
        this.scrollLoadData(res, page);
      })
    }
  },
  //拼接数据
  scrollLoadData: function (res, page) {
    var data = res.items;
    var productData = this.data.productData;
    productData = productData.concat(data)
    this.setData({
      productData: productData,
      page: page,
      pageCount: res._meta.pageCount,
    })
    if (page == this.data.pageCount) {
      this.setData({
        searchLoading: false,
        searchLoadingComplete: true,
      })
    }
  },

  //展开收起作者简介
  flodFn: function () {
    this.setData({
      isFlod: !this.data.isFlod
    })
  },

  /*跳转到商品详情*/
  onProductsItemTap: function (event) {
    var id = author.getDataSet(event, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },

  /*跳转到商品详情*/
  showattrbox: function (e) {
    var id = author.getDataSet(e, 'id');
    wx.navigateTo({
      url: '../product/product?id=' + id
    })
  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {
    return {
      title: this.data.articleInfo.title + ' ' + this.data.articleInfo.category
    }
  }



})